package com.chx.controller;

import com.chx.dto.ProductDto;
import com.chx.dto.ProductPageQueryDto;
import com.chx.entity.Products;
import com.chx.result.PageResult;
import com.chx.result.Result;
import com.chx.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author cp-月入过万
 * @Date 2025/5/30
 */
@RestController
@RequestMapping("/admin/product")
@Api(tags = "商品相关接口")
@Slf4j
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 获取商品列表
     */
    @GetMapping("/list")
    @ApiOperation("获取商品列表")
    public Result<List<Products>> getAllProducts() {
        log.info("获取商品列表");
        return Result.success(productService.getAllProducts());
    }
    /**
     * 添加商品
     */
    @ApiOperation("添加商品")
    @PostMapping
    public Result<String> save(@RequestBody ProductDto productDto) {
        log.info("添加商品");
        productService.save(productDto);
        return Result.success();
    }
    /**
     * 修改商品
     */
    @ApiOperation("修改商品")
    @PutMapping
    public Result<String> update(@RequestBody ProductDto productDto) {
        log.info("修改商品");
        productService.update(productDto);
        return Result.success();
    }
    /**
     * 删除商品
     */
    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除商品");
        productService.delete(id);
        return Result.success();
    }
    /**
     * 根据id查询商品信息
     */
    @ApiOperation("根据id查询商品信息")
    @GetMapping("/{id}")
    public Result<Products> getById(@PathVariable Long id) {
        log.info("根据id查询商品信息");
        return Result.success(productService.getById(id));
    }
    /**
     * 分页查询商品信息
     */
    @ApiOperation("分页查询商品信息")
    @GetMapping("/page")
    public Result<PageResult> page(ProductPageQueryDto productPageQueryDto) {
        log.info("分页查询商品信息");
        return Result.success(productService.page(productPageQueryDto));
    }

}
